package com.kykj.kykj.controller;

import com.kykj.kykj.entity.ResultEntity;
import com.kykj.kykj.entity.RouteEntity;
import com.kykj.kykj.service.impl.RouteServiceImpl;
import com.kykj.kykj.vo.RouteVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("v2/route/")
@CrossOrigin//解决跨域问题
@RestController
public class RouteController {
    @Resource
    RouteServiceImpl routeService;

    /**
     * 添加路由
     * @param routeVo 路由对象
     * @return ResultEntity
     */
    @PostMapping("route")
    private ResultEntity addRoute(@RequestBody RouteVo routeVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(routeService.addRoute(routeVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception exception){
            exception.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("添加失败！");
        }
        return resultEntity;
    }

    /**
     * 删除路由
     * @param menuid 路由id
     * @return ResultEntity
     */
    @DeleteMapping("route/{menuid}")
    private ResultEntity delRoute(@PathVariable("menuid") int menuid){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(routeService.delRoute(menuid)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("删除成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("删除失败！");
            }
        }catch (Exception exception){
            exception.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("删除失败！");
        }
        return resultEntity;
    }
    /**
     * 修改路由
     * @param routeVo 路由对象
     * @return ResultEntity
     */
    @PutMapping("route")
    private ResultEntity updateRoute(@RequestBody RouteVo routeVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(routeService.updateRoute(routeVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception exception){
            exception.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("修改失败！");
        }
        return resultEntity;
    }

    /**
     * 获取Route对象
     * @param menuid menuid
     * @return ResultEntity
     */
    @GetMapping("route")
    private ResultEntity getRouteByMenuId(@RequestParam("menuid") int menuid){
        ResultEntity resultEntity = new ResultEntity();
        try{
            RouteEntity routeEntity = routeService.getRouteByMenuId(menuid);
            if(routeEntity!=null){
                resultEntity.setCode(0);
                resultEntity.setMsg("数据获取成功！");
                resultEntity.setData(routeEntity);
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("数据获取失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("数据获取失败！");
        }
        return resultEntity;
    }

    /**
     * 获取路由列表
     * @param name 路由名称
     * @param controller 控制器
     * @param page 页码
     * @param limit 每页行数
     * @return ResultEntity
     */
    @GetMapping("getroutelist")
    private ResultEntity getRouteList(
            @RequestParam("name") String name,
            @RequestParam("controller") String controller,
            @RequestParam("page") int page,
            @RequestParam("limit") int limit
    ){
        ResultEntity resultEntity = new ResultEntity();
        try{
           int start =(page-1)*limit;//计算起始位置
            List<RouteEntity> list = routeService.getRouteList(name, controller, start, limit);//获取列表
           if(name.equals("") && controller.equals("")){//没有查询条件，就返回所有行数
                resultEntity.setCount(routeService.getRouteCount());
           }else{
               resultEntity.setCount(list.size());//获取列表的大小
           }
           resultEntity.setData(list);
           resultEntity.setMsg("数据获取成功！");
           resultEntity.setCode(0);
        }catch (Exception exception){
            exception.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("数据获取失败！");
        }
        return resultEntity;
    }

}
